package manage

import (
	"lingc/models"
	"lingc/services"
	"lingc/utils" // 导入 common 包
	"log"
	"strings"

	"github.com/gin-gonic/gin"
)

// PageSysLangItem 获取所有条目
func PageSysLangItem(c *gin.Context) {

	/*
		Keywords string `json:"keywords" form:"keywords"`
	*/
	var dataRequest struct {
		Keywords string `json:"keywords" form:"keywords"`
		Page     int    `json:"page" form:"page"`
		PageSize int    `json:"pageSize" form:"pageSize"` // Exported field
	}
	// log.Printf("PageUser: %+v", dataRequest)

	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	where := ""
	if dataRequest.Keywords != "" {
		keyword := "'%" + dataRequest.Keywords + "%'"
		where += " `title` like " + keyword
	}
	list, page, total, err := services.NewSysLangItemService(utils.MysqlClient).PageSysLangItemList(where, dataRequest.Page, dataRequest.PageSize)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	head, _ := services.NewSysLangService(utils.MysqlClient).GetLangList()
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"list": list, "page": page, "total": total, "head": head})
}

// GetUser 获取条目
func GetSysLangItem(c *gin.Context) {

	var dataRequest struct {
		ID string `json:"id" form:"id"`
	}
	where := ""
	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if dataRequest.ID != "" {
		if where != "" {
			where += " and "
		}
		where += " id = " + dataRequest.ID
	}
	data, err := services.NewSysLangItemService(utils.MysqlClient).GetSysLangItem(where)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// CreateUser 创建条目
func CreateSysLangItem(c *gin.Context) {

	/**
	Title     string `json:"title"`
	Detail   string `json:"detail"`
	AppID     int    `json:"app_id"`
	*/
	var createRequest struct {
		Title  string `json:"title" form:"title"`
		Detail string `json:"detail" form:"detail"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.SysLangItemCreate{
		Title: createRequest.Title,
	}
	ids, err := services.NewSysLangItemService(utils.MysqlClient).CreateSysLangItem(data, createRequest.Detail)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": ids})
}

// UpdateUser 更新条目
func UpdateSysLangItem(c *gin.Context) {
	var createRequest struct {
		ID       uint   `json:"id" form:"id"`
		Title    string `json:"title" form:"title"`
		Detail   string `json:"detail" form:"detail"`
		IsUpdate string `json:"is_update" form:"is_update"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}

	if createRequest.IsUpdate == "file" {
		services.NewSysLangDetailService(utils.MysqlClient).GetSysLangList(true)
		services.NewSysLangDetailService(utils.MysqlClient).SetCahceConfigTime(c)
		utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": "success"})
		return
	}

	data := &models.SysLangItemUpdate{
		ID:    createRequest.ID,
		Title: createRequest.Title,
	}
	log.Printf("UpdateSysLangItem: %+v", createRequest.Detail)
	ids, err := services.NewSysLangItemService(utils.MysqlClient).UpdateSysLangItem(data, createRequest.Detail)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": ids})
}

// DeleteUser 删除条目
func DeleteSysLangItem(c *gin.Context) {
	var createRequest struct {
		ID  uint   `json:"id" form:"id"`
		IDs string `json:"ids" form:"ids"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID == 0 && len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID != 0 {
		if err := services.NewSysLangItemService(utils.MysqlClient).DeleteSysLangItem(createRequest.ID); err != nil {
			utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
			return
		}
	}
	if len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	idStrs := strings.Split(createRequest.IDs, ",")
	var ids []string
	for _, idStr := range idStrs {
		idStr = strings.TrimSpace(idStr)
		if idStr == "" {
			continue
		}
		ids = append(ids, idStr)
	}
	// Convert IDs slice to a format suitable for batch deletion
	if len(ids) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "No valid IDs provided"})
		return
	}
	if err := services.NewSysLangItemService(utils.MysqlClient).DeleteSysLangItems(ids); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	// ... 这里添加删除条目的逻辑 ...
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"msg": "Delete data"})
}
